<?php
	//include_once('./include/wx.class.php');
include_once(ROOTDIR.'include/wx.class.php');
class Test_con extends Lxbase {
	public function index(){
		$data=$this->arrdata("*","users");
		$this->smarty->assign("data",$data);
		$this->smarty->display("test.tpl");
	}
	
	public function testkey(){
		$key=$_GET["key"];
		$username=$_GET["uname"];
		$url=$this->basehost."?g=user&m=activityinfo&a=returninfo&username={$username}&keyword=".$key;
		$ret = json_decode($this->call_get($url),true);
		print_r($ret);
		exit;
	}
	
	public function testupload(){
		if($_POST){
			print_r($_POST);
			print_r($_FILES);
			exit;
		}
		$this->smarty->display("upload.tpl");
	}

    public function upusers(){
        $res = $this->arrdata('id,plas','users');
        foreach($res as $k=>$v){
            $plaarr = array();
            $plaarr = explode(',',$v['plas']);
           //    $res[$k]['plas'] = rtrim($v['plas'],',');
          //  $this->updata('users','plas = "' . $res[$k]['plas'] . '"','where id = ' . $res[$k]['id']);
            if($plaarr[0] != '4'){
				if(empty($v['plas'])){
					$res[$k]['plas']=4;
				}else{
                	$res[$k]['plas'] = '4,' . $v['plas'];
				}
               $this->updata('users','plas = "' . $res[$k]['plas'] . '"','where id = ' . $res[$k]['id']);
            }
        }
        echo 'ok';exit;
    }


	//新建门店
	public function add_shop(){
		$sid=$this->userinfo['userid'];  //商户id
		$business_name=$_POST['business_name']; // 门店名称
		$branch_name=$_POST['branch_name']; //分店名称
		$province=$_POST['province'];   //省
		$city=$_POST['city'];            //城市
		$district=$_POST['district'];    //门店所在地区
		$address=$_POST['address'];       //门店所在的详细街道信息
		$telephone=$_POST['telephone'];   //电话
		$categories=$_POST['categories'];  //门店的类型，不同级别的用 ， 号隔开
		$offset_type=1;                      //坐标类型 1 为火星坐标
		$longitude=$_POST['longitude'];     //经度
		$latitude=$_POST['latitude'];       //纬度
 		$photo_list=$_POST['photo_url'];    //门店图片 数组
		$recommend=$_POST['recommend'];     //特别推荐
		$special=$_POST['special'];           //特色服务 入WiFi，外卖服务
		$introduction=$_POST['introduction'];  //商户简介 主要介绍商户信息
		$open_time=$_POST['open_time'];        //营业时间
		$avg_price=$_POST['avg_price'];        //人均价格
		$user_data=$this->viewdata("*","wxuser","where userid={$sid}");
		$wxshop=new Wx();
		if(time()>$user_data['acctokentim']){
			$wxshop->setwxinfo($sid);
			$user_token=$wxshop->acctoken;
		}else{
			$user_token=$user_data['acctoken'];
		}
		$into=array(
				"userid"=>$sid,
				"business_name"=>$business_name,
				'branch_name'=>$branch_name,
				'province'=>$province,
				'city'=>$city,
				'district'=>$district,
				'address'=>$address,
				'telephone'=>$telephone,
				'categories'=>$categories,
				'offset_type'=>$offset_type,
				'longitude'=>$longitude,
				'latitude'=>$latitude,
				'photo_list'=>$photo_list,
				'recommend'=>$recommend,
				'special'=>$special,
				'introduction'=>$introduction,
				'open_time'=>$open_time,
				'avg_price'=>$avg_price,
		);
		$shop_id=$this->setdata("wxshop",$into);
		$url=" 	http://api.weixin.qq.com/cgi-bin/poi/addpoi?access_token=$user_token";
		$data=array('business'=>array("base_info"=>
				array( 'sid'=>$shop_id,
						'business_name'=>$business_name,
						'branch_name'=>$branch_name,
						'province'=>$province,
						'city'=>$city,
						'district'=>$district,
						'address'=>$address,
						'telephone'=>$telephone,
						'categories'=>$categories,
						'offset_type'=>$offset_type,
						'longitude'=>$longitude,
						'latitude'=>$latitude,
						'photo_list'=>$photo_list,
						'recommend'=>$recommend,
						'special'=>$special,
						'introduction'=>$introduction,
						'open_time'=>$open_time,
						'avg_price'=>$avg_price,
				)
										));
		$data=json_encode($data);
		$res=$this->json_decode(call_post($url,$data),true);

		echo json_encode(array('msg'=>'true'));
		exit;
	}

	//查询门店列表
	public function show_shop(){
		$sid=$this->userinfo['userid'];  //商户id
		$user_data=$this->viewdata("*","wxuser","where userid={$sid}");
		$wxshop=new Wx();
		if(time()>$user_data['acctokentim']){
			$wxshop->setwxinfo($sid);
			$user_token=$wxshop->acctoken;
		}else{
			$user_token=$user_data['acctoken'];
		}
		$url="https://api.weixin.qq.com/cgi-bin/poi/getpoilist?access_token=$user_token";
		$data=array('begin'=>0,'limit'=>10);
		$data=json_encode($data);
		$res=json_decode($this->call_post($url,$data),true);
		$base_info=array();
		foreach($res as $k=>$v){
			$base_info[]=$v['business_list'];
		}
		$this->smarty->assign('base_info',$base_info);
		$this->smarty->display('show_shop.tpl');
	}

	//查询门店信息
	public function detail_shop(){
		$sid=$this->userinfo['userid'];  //商户id
		$user_data=$this->viewdata("*","wxuser","where userid={$sid}");
		$shop_data=$this->viewdata("*","wxshop","where userid={$sid}");
		$wxshop=new Wx();
		if(time()>$user_data['acctokentim']){
			$wxshop->setwxinfo($sid);
			$user_token=$wxshop->acctoken;
		}else{
			$user_token=$user_data['acctoken'];
		}
		$url=" 	http://api.weixin.qq.com/cgi-bin/poi/getpoi?access_token=$user_token";
		$poi_id=$shop_data['poi_id'];
		$data=json_encode(array('poi_id'=>$poi_id));
		$res=json_decode($this->call_post($url,$data),true);
		$business=array();
		foreach($res as $k=>$v){
			$business[]=$v['business'];
		}
		$this->smarty->assign('business',$business);
		$this->smarty->display('detail_shop.tpl');
	}

	//删除门店信息
	public function del_shop(){
		$id=$_GET['id'];
		$sid=$this->userinfo['userid'];  //商户id
		$user_data=$this->viewdata("*","wxuser","where userid={$sid}");
		$shop_data=$this->viewdata("*","wxshop","where userid={$sid}");
		$wxshop=new Wx();
		if(time()>$user_data['acctokentim']){
			$wxshop->setwxinfo($sid);
			$user_token=$wxshop->acctoken;
		}else{
			$user_token=$user_data['acctoken'];
		}
		$url=" 	https://api.weixin.qq.com/cgi-bin/poi/delpoi?access_token=$user_token";
		$poi_id=$shop_data['poi_id'];
		$data=json_encode(array('poi_id'=>$poi_id));
		$res=json_decode($this->call_post($url,$data),true);
		$this->deldata("wxshop","where $id={$id} and userid={$sid}");
	}

	//获取门店类目表
	public function get_cate(){
		$sid=$this->userinfo['userid'];  //商户id
		$user_data=$this->viewdata("*","wxuser","where userid={$sid}");
		$wxshop=new Wx();
		if(time()>$user_data['acctokentim']){
			$wxshop->setwxinfo($sid);
			$user_token=$wxshop->acctoken;
		}else{
			$user_token=$user_data['acctoken'];
		}
		$url="http://api.weixin.qq.com/cgi-bin/poi/getwxcategory?access_token=$user_token";
		$cate_data=json_decode($this->call_get($url),true);
		$this->smarty->assign('cate_data',$cate_data);
		$this->smarty->display('detail_shop.tpl');
	}

	//修改门店信息
	public function update_shop(){
		$sid=$this->userinfo['userid'];  //商户id
		$user_data=$this->viewdata("*","wxuser","where userid={$sid}");
		$shop_data=$this->viewdata("*","wxshop","where userid={$sid}");
		$poi_id=$shop_data['poi_id'];
		$wxshop=new Wx();
		if(time()>$user_data['acctokentim']){
			$wxshop->setwxinfo($sid);
			$user_token=$wxshop->acctoken;
		}else{
			$user_token=$user_data['acctoken'];
		}
		$url="https://api.weixin.qq.com/cgi-bin/poi/updatepoi?access_token=$user_token";
		  $data=array(
		          "poi_id "=>$poi_id,
                  "telephone"=>$_POST['telephone'],
                  "photo_list"=>'[{"photo_url"=>"https:// XXX.com"},{"photo_url"=>"https://XXX.com"}]',
                  "recommend"=>"麦辣鸡腿堡套餐，麦乐鸡，全家桶",
                  "special"=>"免费wifi，外卖服务",
                  "introduction"=>"麦当劳是全球大型跨国连锁餐厅，1940 年创立于美国，在世界上大约拥有3 万间分店。主要售卖汉堡包，以及薯条、炸鸡、汽水、冰品、沙拉、水果等快餐食品",
                  "open_time"=>"8:00-20:00",
                  "avg_price"=>35
			);
	}

	//微信上传图片接口
	public function up_img(){
		$tempurl = $this->settempfile($_FILES["img"]);
		$sid=$this->userinfo['userid'];  //商户id
		$user_data=$this->viewdata("*","wxuser","where userid={$sid}");
		$wxshop=new Wx();
		if(time()>$user_data['acctokentim']){
			$wxshop->setwxinfo($sid);
			$user_token=$wxshop->acctoken;
		}else{
			$user_token=$user_data['acctoken'];
		}
		$url="https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=$user_token";
		$data["buffer"] = "@".$tempurl;
		$img_url=json_decode($this->call_post($url,$data),true);
		echo json_encode(array('img_url'=>$img_url,'msg'=>'true'));
		exit;
	}


	//创建卡券
	public function add_card(){

		$sid=$this->userinfo['userid'];  //商户id
		$user_data=$this->viewdata("*","wxuser","where userid={$sid}");
		$wxshop=new Wx();
		if(time()>$user_data['acctokentim']){
			$wxshop->setwxinfo($sid);
			$user_token=$wxshop->acctoken;
		}else{
			$user_token=$user_data['acctoken'];
		}
		$url="https://api.weixin.qq.com/card/create?access_token=$user_token";
//		$card_type=$_POST['card_type'];
//		$logo_url=$_POST['logo_url'];
//		$brand_name=$_POST['brand_name'];
//		$code_type=$_POST['code_type'];
//		$title=$_POST['title'];
//		$sub_title=$_POST['sub_title'];
//		$color=$_POST['color'];
//		$notice=$_POST['notice'];
//		$service_phone=$_POST['service_phone'];
//		$description=$_POST['description'];
//		$type=$_POST['type'];
//		$begin_timestamp=$_POST['begin_timestamp'];
//		$end_timestamp=$_POST['end_timestamp'];
//		$quantity=$_POST['quantity'];
//		$get_limit=$_POST['get_limit'];
//		$use_custom_code=$_POST['use_custom_code'];
//		$bind_openid=$_POST['bind_openid'];
//		$can_share=$_POST['can_share'];
//		$can_give_friend=$_POST['can_give_friend'];
//		$deal_detail=$_POST['deal_detail'];
//		$least_cost=$_POST['least_cost'];
//		$reduce_cost=$_POST['reduce_cost'];
//		$discount=$_POST['discount'];
//		$gift=$_POST['gift'];
//		$default_detail=$_POST['default_detail'];

		$card_type='GROUPON';
		$logo_url='1.JPG';
		$brand_name='海底捞';
		$code_type='CODE_TYPE_TEXT';
		$title='132元双人火锅套餐';
		$sub_title='周末狂欢必备';
		$color='Color010';
		$notice='使用时向服务员出示此券';
		$service_phone='020-88888888';
		$description='不可与其他优惠同享\n如需团购券发票，请在消费时向商户提出\n店内均可使用，仅限堂食';
		$type='DATE_TYPE_FIX_TIME_RANGE';
		$begin_timestamp=1455689853;
		$end_timestamp=1455699853;
		$quantity=500000;
		$get_limit=3;
		$use_custom_code=false;
		$bind_openid=false;
		$can_share=true;
		$can_give_friend=true;
		$deal_detail='以下锅底2选1（有菌王锅、麻辣锅、大骨锅、番茄锅、清补 凉锅、酸菜鱼锅可选）：\n大锅1份 12元\n小锅2份 16元 ';
		$least_cost=$_POST['least_cost'];
		$reduce_cost=$_POST['reduce_cost'];
		$discount=$_POST['discount'];
		$gift=$_POST['gift'];
		$default_detail=$_POST['default_detail'];
		//团购post
		$groupon_data=array(
			'card'=>array(
					"card_type"=>$card_type,
					"groupon"=>array(
							"base_info"=>array(
									"logo_url"=>$logo_url,
									"brand_name"=>$brand_name,
									"code_type"=>$code_type,
									"title"=>$title,
									"sub_title"=>$sub_title,
									"color"=>$color,
									"notice"=>$notice,
									"service_phone"=>$service_phone,
									"description"=>$description,
									"date_info"=>array(
											"type"=>$type,
											"begin_timestamp"=>$begin_timestamp,
                   							"end_timestamp"=>$end_timestamp,
									),
									"sku"=>array(
											"quantity"=>$quantity,
									),
									"get_limit"=>$get_limit,
								    "use_custom_code"=>$use_custom_code,
								    "bind_openid"=>$bind_openid,
								    "can_share"=>$can_share,
								    "can_give_friend"=>$can_give_friend,
							),
							"deal_detail"=>$deal_detail,
					),
			),
		);
		//代金券post
		$cash_data=array(
				'card'=>array(
						"card_type"=>$card_type,
						"CASH"=>array(
								"base_info"=>array(
										"logo_url"=>$logo_url,
										"brand_name"=>$brand_name,
										"code_type"=>$code_type,
										"title"=>$title,
										"sub_title"=>$sub_title,
										"color"=>$color,
										"notice"=>$notice,
										"service_phone"=>$service_phone,
										"description"=>$description,
										"date_info"=>array(
												"type"=>$type,
												"begin_timestamp"=>$begin_timestamp,
												"end_timestamp"=>$end_timestamp,
										),
										"sku"=>array(
												"quantity"=>$quantity,
										),
										"get_limit"=>$get_limit,
										"use_custom_code"=>$use_custom_code,
										"bind_openid"=>$bind_openid,
										"can_share"=>$can_share,
										"can_give_friend"=>$can_give_friend,
								),
								"reduce_cost"=>$reduce_cost,
								'least_cost'=>$least_cost,
						),
				),
		);
		//折扣券post
		$discount_data=array(
				'card'=>array(
						"card_type"=>$card_type,
						"DISCOUNT"=>array(
								"base_info"=>array(
										"logo_url"=>$logo_url,
										"brand_name"=>$brand_name,
										"code_type"=>$code_type,
										"title"=>$title,
										"sub_title"=>$sub_title,
										"color"=>$color,
										"notice"=>$notice,
										"service_phone"=>$service_phone,
										"description"=>$description,
										"date_info"=>array(
												"type"=>$type,
												"begin_timestamp"=>$begin_timestamp,
												"end_timestamp"=>$end_timestamp,
										),
										"sku"=>array(
												"quantity"=>$quantity,
										),
										"get_limit"=>$get_limit,
										"use_custom_code"=>$use_custom_code,
										"bind_openid"=>$bind_openid,
										"can_share"=>$can_share,
										"can_give_friend"=>$can_give_friend,
								),
								"discount"=>$discount,
						),
				),
		);
		//兑换券post
		$gift_data=array(
				'card'=>array(
						"card_type"=>$card_type,
						"GIFT"=>array(
								"base_info"=>array(
										"logo_url"=>$logo_url,
										"brand_name"=>$brand_name,
										"code_type"=>$code_type,
										"title"=>$title,
										"sub_title"=>$sub_title,
										"color"=>$color,
										"notice"=>$notice,
										"service_phone"=>$service_phone,
										"description"=>$description,
										"date_info"=>array(
												"type"=>$type,
												"begin_timestamp"=>$begin_timestamp,
												"end_timestamp"=>$end_timestamp,
										),
										"sku"=>array(
												"quantity"=>$quantity,
										),
										"get_limit"=>$get_limit,
										"use_custom_code"=>$use_custom_code,
										"bind_openid"=>$bind_openid,
										"can_share"=>$can_share,
										"can_give_friend"=>$can_give_friend,
								),
								"gift"=>$gift,
						),
				),
		);
		//优惠券post
		$general_coupon_data=array(
				'card'=>array(
						"card_type"=>$card_type,
						"GENERAL_COUPON"=>array(
								"base_info"=>array(
										"logo_url"=>$logo_url,
										"brand_name"=>$brand_name,
										"code_type"=>$code_type,
										"title"=>$title,
										"sub_title"=>$sub_title,
										"color"=>$color,
										"notice"=>$notice,
										"service_phone"=>$service_phone,
										"description"=>$description,
										"date_info"=>array(
												"type"=>$type,
												"begin_timestamp"=>$begin_timestamp,
												"end_timestamp"=>$end_timestamp,
										),
										"sku"=>array(
												"quantity"=>$quantity,
										),
										"get_limit"=>$get_limit,
										"use_custom_code"=>$use_custom_code,
										"bind_openid"=>$bind_openid,
										"can_share"=>$can_share,
										"can_give_friend"=>$can_give_friend,
								),
								"default_detail"=>$default_detail,
						),
				),
		);
		if($card_type=='GROUPON'){
			//团购券
			$data=$groupon_data;
		}elseif($card_type=='CASH'){
			//代金券
			$data=$cash_data;
		}elseif($card_type=='DISCOUNT'){
			//折扣券
			$data=$discount_data;
		}elseif($card_type=='GIFT'){
			//兑换券
			$data=$gift_data;
		}elseif($card_type=='GENERAL_COUPON'){
			//优惠券
			$data=$general_coupon_data;
		}
		$data=json_encode($data,JSON_UNESCAPED_UNICODE);
		$res=json_decode($this->call_post($url,$data),true);

		$card_id=$res['card_id'];
		//var_dump($card_id);exit;
		//插入主表信息
		if($card_type=='GROUPON'){
			//团购券
			$into="userid={$sid},card_type='{$card_type}',card_id='{$card_id}',logo_url='{$logo_url}',brand_name='{$brand_name}',code_type='{$code_type}',
			      title='{$title}',sub_title='{$sub_title}',color='{$color}',notice='{$notice}',description='{$description}',
			      type='{$type}',begin_timestamp='{$begin_timestamp}',end_timestampo='{$end_timestamp}',
			      quantity={$quantity},deal_detail='{$deal_detail}'";


		}elseif($card_type=='CASH'){
			//代金券
			$into="userid={$sid},card_type='{$card_type}',card_id='{$card_id}',logo_url='{$logo_url}',brand_name='{$brand_name}',code_type='{$code_type}',
			      title='{$title}',sub_title='{$sub_title}',color='{$color}',notice='{$notice}',description='{$description}',quantity={$quantity},
			      type='{$type}',begin_timestamp='{$begin_timestamp}',end_timestampo='{$end_timestamp}',
			      least_cost='{$least_cost}',reduce_cost='{$reduce_cost}'";
		}elseif($card_type=='DISCOUNT'){
			//折扣券
			$into="userid={$sid},card_type='{$card_type}',card_id='{$card_id}',logo_url='{$logo_url}',brand_name='{$brand_name}',code_type='{$code_type}',
			      title='{$title}',sub_title='{$sub_title}',color='{$color}',notice='{$notice}',service_phone='{$service_phone}',
			      description='{$description}',type='{$type}',begin_timestamp='{$begin_timestamp}',end_timestampo='{$end_timestamp}',
			      quantity={$quantity}, discount='{$discount}'";

		}elseif($card_type=='GIFT'){
			//兑换券
			$into="userid={$sid},card_type='{$card_type}',card_id='{$card_id}',logo_url='{$logo_url}',brand_name='{$brand_name}',code_type='{$code_type}',
			      title='{$title}',sub_title='{$sub_title}',color='{$color}',notice='{$notice}',service_phone='{$service_phone}',
			      description='{$description}',type='{$type}',begin_timestamp='{$begin_timestamp}',end_timestampo='{$end_timestamp}',
			      quantity={$quantity},gift='{$gift}'";

		}elseif($card_type=='GENERAL_COUPON'){
			//优惠券
			$into="userid={$sid},card_type='{$card_type}',card_id='{$card_id}',logo_url='{$logo_url}',brand_name='{$brand_name}',code_type='{$code_type}',
			      title='{$title}',sub_title='{$sub_title}',color='{$color}',notice='{$notice}',service_phone='{$service_phone}',
			      description='{$description}',type='{$type}',begin_timestamp='{$begin_timestamp}',end_timestampo='{$end_timestamp}',
			      quantity={$quantity},default_detail='{$default_detail}'";
		}
		$card_type_id1=$this->setdata("wxcard_baseinfo",$into);
		$into2="baseinfo_id='{$card_type_id1}',can_share='{$can_share}',can_give_friend='{$can_give_friend}',service_phone='{$service_phone}',
				get_limit={$get_limit},use_custom_code='{$use_custom_code}',bind_openid='{$bind_openid}'";
		$card_type_id2=$this->setdata("wxcard",$into2);
		echo "做好了";exit;
		header('location:?g=wx&m=weixin&a=show_card');
	}

	//批量查询卡券列表信息
	public function showd_card(){
		$sid=$this->userinfo['userid'];  //商户id
		$user_data=$this->viewdata("*","wxuser","where userid={$sid}");
		$card_data=$this->viewdata("card_type,title,type","wxcard_baseinfo","where userid={$sid}");
		$wxshop=new Wx();
		if(time()>$user_data['acctokentim']){
			$wxshop->setwxinfo($sid);
			$user_token=$wxshop->acctoken;
		}else{
			$user_token=$user_data['acctoken'];
		}
		$url="https://api.weixin.qq.com/card/batchget?access_token=$user_token";
		$data=array(
				'offset'=>0,
				'count'=>20,
				'status_list'=>array("CARD_STATUS_VERIFY_OK", "CARD_STATUS_DISPATCH"),
		);
		$data=json_encode($data);
		$res=json_decode($this->call_post($url,$data),true);
		$this->smarty->assign('user_data',$user_data);
		$this->smarty->display("show_card.tpl");
	}

	//查询卡券列表信息
	public function show_card(){
		$sid=$this->userinfo['userid'];  //商户id
		$card_data=$this->viewdata("card_type,title,type","wxcard_baseinfo","where userid={$sid}");
		$this->smarty->assign('user_data',$card_data);
		$this->smarty->display("show_card.tpl");
	}

	//删除卡券
	public function del_card(){
		$sid=$this->userinfo['userid'];  //商户id
		$user_data=$this->viewdata("*","wxuser","where userid={$sid}");
		$card_id=$this->viewdata("card_id","wxcard_baseinfo","where userid={$sid}");
		$wxshop=new Wx();
		if(time()>$user_data['acctokentim']){
			$wxshop->setwxinfo($sid);
			$user_token=$wxshop->acctoken;
		}else{
			$user_token=$user_data['acctoken'];
		}
		$url="https://api.weixin.qq.com/card/delete?access_token=$user_token";
		$data=json_encode(array('card_id'=>$card_id));
		$res=json_decode($this->call_post($url,$data),true);
		echo json_decode(array('msg'=>true));
		exit;
	}

	//查询详情
	public function detail_card(){
		//$id=$_POST['id'];
		//$id="pTk4-wdz0K--Tf1JUkYNAieikasU";
		$sid=$this->userinfo['userid'];  //商户id
		$card_data=$this->viewdata("*","wxcard_baseinfo","where userid={$sid} and card_id='{$id}'");
		//var_export($card_data);exit;
		$this->smarty->assign('user_data',$card_data);
		$this->smarty->display("detail_card.tpl");
	}
}
